library OrbitalArtillery initializer init requires LegacySystem

	private function OrbitalArtilleryConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A02D'
	endfunction

	private function OrbitalArtillery takes nothing returns nothing
		local integer i = 1
		local player p = GetOwningPlayer(GetTriggerUnit())
		local location l = GetSpellTargetLoc()
		local real x = GetLocationX(l)
		local real y = GetLocationY(l)
		local rect r = Rect(x-300,y-300,x+300,y+300)
		local group g

		set udg_myinteger = 1
		if GetRandomInt(1,30)==1 then
			call GeneralText(3.0, "|cfff3ad00Forward Observer|r : Fire for Effect." )
		endif
		call EarShockArea(x,y,800)
		call EnumItemsInRect(r,null,function killitem)
		call EnumDestructablesInRect(r,null,function killtree)
		//call TerrainDeformationRippleBJ( 4, true, l, 100.0, 500.0, 24.0, 1, 60.0 )

		// aoe slow used to be here before.
		
		loop
			exitwhen i>2
			call PolledWait(1.)
			call DestroyEffect(AddSpecialEffect( "Abilities\\Spells\\Other\\Doom\\DoomDeath.mdl",x,y ))
			call DestroyEffect(AddSpecialEffect( "Abilities\\Spells\\Other\\Monsoon\\MonsoonRain.mdl",x,y ))
			set i=i+1
		endloop
		call RemoveLocation(l)
		set l = null
		call RemoveRect(r)
		set r = null
	endfunction

	private function OrbitalArtilleryLearn takes nothing returns boolean
		local integer i = GetLearnedSkill()
		local unit u = null 
		local integer level 
		local player p = null 
		if i == 'A05I' then

			set u = GetTriggerUnit()
			set level = GetUnitAbilityLevel(u, 'A05I') 
			set p = GetOwningPlayer(u)
			call SetUnitAbilityLevel(u, 'A02D', level)
			call SetPlayerTechResearched(p, 'R007', level)
			set p = null 
			set u = null
		endif
		return false
	endfunction
	
	private function init takes nothing returns nothing
		local trigger t 
		set t = CreateTrigger()
		
		call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
		call TriggerAddCondition(t, Condition(function OrbitalArtilleryConditions))
		call TriggerAddAction(t, function OrbitalArtillery)
		
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_HERO_SKILL)
		call TriggerAddCondition(t, Condition(function OrbitalArtilleryLearn))
	endfunction

endlibrary